package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.query.SearchQueryObject;
import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.repository.RegionRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private RegionRepository regionRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {

    }

    @Override
    public void delete(String id) {

    }

    @Override
    public Destination select(String id) {
        return destinationRepository.findById(id).get();
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();



        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(".*" + qo.getKeyword() + ".*"));
        }

        System.out.println(qo.getParentId());

        if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }

        long total = mongoTemplate.count(query, Destination.class);
        if (total == 0) {
            return Page.empty();
        }

        PageRequest pageable = PageRequest.of(qo.getCurrentPage() -1, qo.getPageSize()
                , Sort.Direction.ASC, "_id");
        query.with(pageable);

        List<Destination> destinations = mongoTemplate.find(query, Destination.class, "destination");

        return new PageImpl<Destination>(destinations, pageable, total);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String id) {
        Region region = regionRepository.findById(id).get();
        List<String> refIds = region.getRefIds();
        return destinationRepository.findByIdIn(refIds);
    }

    @Override
    public List<Destination> findByParentId(String parentId) {
        if (!(StringUtils.hasLength(parentId))) {
            return Collections.emptyList();
        }

        List<Destination> toats = new ArrayList<>();
//        Destination destination = this.select(parentId);
//        toats.add(destination);
//
//        for (int i = 0; i < 4; i++) {
//            if (StringUtils.hasLength(destination.getParentId())) {
//                destination = destinationRepository.findById(destination.getParentId()).get();
//                toats.add(destination);
//            }
//        }

        createToast(toats, parentId);

        Collections.reverse(toats);
        return toats;
    }

    @Override
    public void updateInfo(String id, String info) {
        if (StringUtils.hasLength(id)) {
            Destination destination = destinationRepository.findById(id).get();
            destination.setInfo(info);
            destinationRepository.save(destination);
        }
    }

    @Override
    public List<Destination> search(String regionId) {

        List<Destination> list = new ArrayList<>();
        //区分国内
        if ("-1".equals(regionId)) {
            //查询所有省份
            list = destinationRepository.findByParentName("中国");
        } else {
            list = this.getDestByRegionId(regionId);
        }

        //找儿子
        for (Destination destination : list) {
            //显示前五个(仅做演示)
            Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "_id");
            List<Destination> childern = destinationRepository.findByParentId(destination.getId(), pageable);
            destination.setChildren(childern);
        }

        return list;
    }

    @Override
    public List<Destination> listAll() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> listByKeyword(SearchQueryObject qo) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.andOperator(
                Criteria.where("name").regex(".*" + qo.getKeyword() + ".*"),
                Criteria.where("info").regex(".*" + qo.getKeyword() + ".*")
        );
        query.addCriteria(criteria);
        List<Destination> destinations = mongoTemplate.find(query, Destination.class, "destination");
        return destinations;
    }

    @Override
    public Destination findByKeyword(SearchQueryObject qo) {
        return destinationRepository.findByName(qo.getKeyword());
    }

    private void createToast(List<Destination> list, String parentId) {
        Destination dest = this.select(parentId);
        list.add(dest);

        if (StringUtils.hasLength(dest.getParentId())) {
            createToast(list, dest.getParentId());
        }
    }
}
